var advList=[];  //广告数据
var serviceUrl = $api.getStorage('serviceUrl'); //服务器地址

var grabbbuylist=[];  //抢购数据
var procate=[];  //商品类别


apiready = function(){
  getindexData();
 //  openScrollPicture();
 
  api.setRefreshHeaderInfo({
        visible: true,
        // loadingImgae: 'wgt://image/refresh-white.png',
        bgColor: '#f2f2f2',
        textColor: '#4d4d4d',
        textDown: '下拉刷新...',
        textUp: '松开刷新...',
        showTime: true
    }, function (ret, err) {
        getindexData();
        
        api.refreshHeaderLoadDone();
    });
 
 

};


//获取首页信息
function getindexData(){
   
   do_ajax('/app/home','get',{},function(ret,err){
       
       if(ret){
           advList =ret.ad1;   //广告
           grabbbuylist=ret.activity;  //抢购
           procate=ret.category;    //商品类别
           
	       openScrollPicture(); 
	       showindexData();
	       
	       api.hideProgress();
       }else{
            api.alert({
	            msg:('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode)
	        });
	        api.hideProgress();
       
       }
   
   
   });
   


};


//设置滑动图片
function openScrollPicture(){

    var paths = [];

   
    for(var i=0;i<advList.length;i++){
	     paths.push(serviceUrl+advList[i].img);
	}

	var obj = api.require('UIScrollPicture');
	
//	var $right = $api.byId('rightw');
//	var rightwidth=$api.offset($right).w;   //右边宽度
//	var picturewidth=api.winWidth-rightwidth;  
	
	
	obj.open({
	    rect: {
	        x: 0, y: 0, w:api.winWidth, h:200
	    },
	    data: {
	        paths: paths,
	    },
	    styles: {
	        indicator: {
	            align: 'right',
	            color: '#666',
	            activeColor: '#ffaa43'
	        }
	    },
	    placeholderImg: 'widget://res/slide1.jpg',
	    contentMode: 'scaleToFill',
	    interval:5,
	    loop: true,
	    fixedOn: 'index',
	    fixed: false
	}, function(ret, err){
	     if(ret.status){
	        if(ret.eventType == 'click'){
	            //点击图片的操作
	            //alert(ret.index);
	            var description = advList[ret.index].description;
	            
				handleClick(ret.index, description);
	        }
	     }
	});

    
    
    
};


//显示数据
function showindexData(){
    
    for(var i=0;i<grabbbuylist.length;i++){
        
        var tid=grabbbuylist[i].order;
        $api.text($api.byId('qg'+tid+'title'), grabbbuylist[i].name);
        $api.attr($api.byId('qg'+tid+'img'), 'src', grabbbuylist[i].thumbnail?serviceUrl+ grabbbuylist[i].thumbnail:'../../image/noImage.png');
    
    }
    
    $api.text($api.byId('qg1protitle'), grabbbuylist[0].description);
    
    
   // $api.text($api.byId('qg1title'), grabbbuylist[0].name);
   // $api.attr($api.byId('qg1img'), 'src', grabbbuylist[0].thumbnail?serviceUrl+ grabbbuylist[0].thumbnail:'../../image/noImage.png');
    
   for(var i=0;i<procate.length;i++){
        
        $api.text($api.byId('cate'+i+'title'), procate[i].name);
        $api.attr($api.byId('cate'+i+'img'), 'src', procate[i].icon?serviceUrl+ procate[i].icon:'../../image/noImage.png');
    
    }
    
   
   
};

//抢购页面
function openGrabbuy(id){
    api.openWin({
	    name: 'grabbuy',
	    url: 'grabbuy_win.html',
	    pageParam: {activity:grabbbuylist[id-1]},
    });
};

//商品列表页面
function openprolist(id){
    
    api.openWin({
	    name: 'productlist',
	    url: '../shop/productlist.html',
	    pageParam: {parentId:procate[id]._id,childList:procate[id].childs}
    });
    
};



//打开框架页
function openfram(fname,fid){

    //调用首页切换框架的方法
     api.execScript({
         name:'root',
	     script: 'randomSwitchBtn("this",\''+fname+'\','+fid+');'
     });

};


//是否登陆
function checkLogin() {
	if (!$api.getStorage('account')) {
		api.openWin({
	        name: 'login',
	        url: '../shop/login.html'
	    });
	    return false;
	}
	return true;
}
//是否认证状态
function checkStatus() {
	if ($api.getStorage('member2').status == '未认证') {
		api.toast({
	        msg:'亲，您还未通过小区身份认证哦',
	        location:'middle'
        });
        return false;
	}
	return true;
}



//物业缴费
function openFee() {

	if (!checkLogin()) return;
	if (!checkStatus()) return;
	
	api.openWin({
	    name: 'fee',
	    url: 'property_fee.html',
	    vScrollBarEnabled: false
    });
}

//物业账单
function openBill() {

	if (!checkLogin()) return;
	if (!checkStatus()) return;
	
	api.openWin({
	    name: 'bill',
	    url: 'property_bill.html'
    });
}

//物业公告
function openAnnounce() {

	if (!checkLogin()) return;
	if (!checkStatus()) return;

	var compounds = $api.getStorage('member2').compounds;
	api.openWin({
	    name: 'articlelist',
	    url: 'articlelist.html',
	    pageParam: {compounds: compounds, type: "物业公告"}
    });
}


//维修通知
function openNotice() {

	if (!checkLogin()) return;
	if (!checkStatus()) return;
	
	var compounds = $api.getStorage('member2').compounds;
	api.openWin({
	    name: 'articlelist',
	    url: 'articlelist.html',
	    pageParam: {compounds: compounds, type: "维修通知"}
    });
}


//72商城
function openMall(){
   api.openWin({
        name: 'shop',
        url: "../shop/main.html",
        bounces: false,
        vScrollBarEnabled:false,
        hScrollBarEnabled:false,
        reload: true,
       }
    );

}



//688套餐
function openredo(){
    api.openWin({
        name: 'tc1',
//      url: "../redo/tc1_win.html",
		url: 'openWebPage.html',
		pageParam: {'title':'72家装','url':'http://www.72gs.net/redo/package?source=app'},
        bounces: false,
        vScrollBarEnabled:false,
        hScrollBarEnabled:false,
        reload: true,
       }
    );
};

//文化教育
function openEducation(){
	api.openWin({
        name: 'productlist',
        url: '../shop/productlist.html',
        pageParam: {'urlString':'?category=文化教育','title':'文化教育'},
        opaque: true,
        vScrollBarEnabled:false
    });
};



//精品案例
function openredocase(){
     api.openWin({
        name: 'tc1',
//      url: "../redo/case.html",
		url: 'openWebPage.html',
		pageParam: {'title':'模板展示','url':'http://www.72gs.net/redo/model/'},
        bounces: false,
        vScrollBarEnabled:false,
        hScrollBarEnabled:false,
        reload: true,
     });
};


//家装服务咨询
function openredoconsult(){
    api.openWin({
        name: 'consult',
        url: "../redo/consult.html",
        bounces: false,
        vScrollBarEnabled:false,
        hScrollBarEnabled:false,
        reload: true,
     });
};

//商品列表
function openProductList(param){
	api.openWin({
        name: 'productlist',
        url: '../shop/productlist.html',
        pageParam: param,
        opaque: true,
        vScrollBarEnabled:false
    });
}

//点击事件
function handleClick(index, type){
	var data = advList[index];

	if(type == '旅游出行'){
		openWin('travel');
	}
	else if(type == '话费充值'){
		openWin('recharge');
	}
	else if(type == '送水'){
		openWin('lifeservice_water');
	}
	else if(type == '家政服务'){
		openProductList({'urlString':'?category=家政服务','title':'家政服务'})
	}
	else if(type == '家电维修'){
		openWin('lifeservice_list',{'type':'家电维修'})
	}
	else if(type == '文化教育'){
		openEducation();
	}
	else if(type == '医疗服务'){
		openProductList({'urlString':'?category=医疗服务','title':'医疗服务'});
	}
	else if(type == '688经典套餐'){
		openredo();
	}
	else if(type == '精品案例'){
		openredocase();
	}
	else if(type == '商品列表'){
	    openProductList({'parentId':data.proid,'isNeedGetChild':true})
	}
	else if(type == '签证'){
		openWin('visa');
	}
	else if(type == '72商城'){
		openMall();
	}
	else if(type == '商品详情'){
		do_ajax('/app/productshow?id='+data.proid, 'get', {}, function(ret,err){
			if(ret){
				//跳转页面
				api.openWin({
				    name: 'productinfo',
				    url: '../shop/productinfo.html',
				    pageParam: {product:ret}
			    });
			}else{
				api.alert({
					msg:('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode)
		        });
			}
   			api.hideProgress(); 
		});
	}
	else if(type == '火热进行'){
		do_ajax('/app/activity/info?name='+data.proid, 'get', {}, function(ret,err){
			if(ret){
				//跳转页面
				api.openWin({
				    name: 'grabbuy',
				    url: 'grabbuy_win.html',
				    pageParam: {activity:ret},
			    });
			}else{
				api.alert({
					msg:('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode)
		        });
			}
   			api.hideProgress(); 
		});
	}
}

